﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using System.Data;

namespace ExpertMIS.Admin.UserControl
{
    public partial class TroubleForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RefreshIdiom();
                RefreshTroubleType();
            }
        }

        protected void ddlIdiom_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txReply.Text = ddlIdiom.SelectedValue;
        }

        #endregion

        #region 公有方法


        public TroubleEntity Entity
        {
            get
            {
                return ViewState["_TroubleEntity_"] as TroubleEntity;
            }
            set
            {
                ViewState["_TroubleEntity_"] = value;
            }
        }

        public void LoadTrouble(string tblOID)
        {
            TroubleBroker broker = new TroubleBroker();
            Hashtable htQuery = new Hashtable();

            htQuery["OID"] = tblOID;
            Entity = broker.GetEntity(htQuery) as TroubleEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as TroubleEntity;
            }
            else
            {
                txTitle.Text = Entity.Title;
                txAddress.Text = Entity.Address;

                RefreshTroubleType();
                for (int i = 0; i <= rblType.Items.Count; i++)
                {
                    ListItem item = rblType.Items[i];
                    if (item.Value == Entity.Type)
                    {
                        rblType.SelectedIndex = i;
                        break;
                    }
                }

                txName.Text = Entity.UserName;
                txQContent.Text = Entity.Content;
                rblIsPassed.SelectedValue = Entity.IsPassed.Value ? "true" : "false";
                txReply.Text = Entity.Reply;
            }
        }

        public bool SaveTrouble()
        {
            try
            {
                TroubleBroker broker = new TroubleBroker();
                if (txReply.Text.Trim().Length > 0)
                {
                    Entity.Reply = txReply.Text;
                    Entity.ReplyDate = DateTime.Now;
                }

                Entity.IsPassed = bool.Parse(rblIsPassed.SelectedValue);
                broker.SaveEntity(Entity, null);
                return true;
            }
            catch
            {
                // 显示错误信息
                return false;
            }
        }

        /// <summary>
        /// 刷新惯用语
        /// </summary>
        public void RefreshIdiom()
        {
            ADMIDIOMBroker broker = new ADMIDIOMBroker();

            IList<ADMIDIOMEntity> list =  broker.GetAllIdioms();

            if(list == null)
            {
                list = new List<ADMIDIOMEntity>();
            }

            ADMIDIOMEntity entity = new ADMIDIOMEntity();
            entity.Content = "";
            list.Insert(0, entity);

            ddlIdiom.DataSource = list;
            ddlIdiom.DataTextField = "Content";
            ddlIdiom.DataValueField = "Content"; //只需要内容即可
            ddlIdiom.DataBind();
        }

        public void RefreshTroubleType()
        {
            SYS.Common.Broker.CodeManagerBroker codeBroker = new SYS.Common.Broker.CodeManagerBroker();
            DataTable table = codeBroker.GetCodeDataByName("TroubleType");
            if (table != null && table.Rows.Count > 0)
            {
                rblType.DataSource = table;
                rblType.DataTextField = "Name";
                rblType.DataValueField = "Code";
                rblType.DataBind();
            }
        }

        #endregion
    }
}